package cn.iocoder.yudao.module.member.controller.admin.smsCode;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyRespVO;
import cn.iocoder.yudao.module.member.controller.admin.smsCode.vo.AuthSendSmsReqVO;
import cn.iocoder.yudao.module.member.controller.admin.smsCode.vo.SmsCodePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.smsCode.vo.SmsCodeRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodePageReqDTO;
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO;
import cn.iocoder.yudao.module.system.service.sms.SmsCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;

@Api(tags = "管理后台 -运营端- 手机验证码")
@RestController
@RequestMapping("/member/smsCode")
@Validated
@Slf4j
public class SmsCodeController {

    @Resource
    private SmsCodeService smsCodeService;

    @Resource
    private CompanyService companyService;

    @PostMapping("/send")
    @ApiOperation(value = "运营-发送手机验证码")
    public CommonResult<CompanyRespVO> send(@RequestBody @Valid AuthSendSmsReqVO reqVO) {
        SmsCodeSendReqDTO sendReqDTO=new SmsCodeSendReqDTO();
        BeanUtils.copyProperties(reqVO,sendReqDTO);
        sendReqDTO.setCreateIp(getClientIP());
        sendReqDTO.setSendChannel(1);
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(null!=loginUser){
            sendReqDTO.setUserName(loginUser.getUsername());
        }
        CompanyDO company = companyService.getCompanyByPhone(reqVO.getMobile());
        sendReqDTO.setTenantId(company.getTenantId());
        String code = smsCodeService.sendSmsCode(sendReqDTO);
        CompanyRespVO respVO=new CompanyRespVO();
        BeanUtils.copyProperties(company,respVO);
        respVO.setSmsCode(code);
        return success(respVO);
    }


    @PostMapping("/page")
    @ApiOperation("获取后台验证法发送信息")
    @TenantIgnore
    public CommonResult<PageResult<SmsCodeRespVO>> page(@RequestBody SmsCodePageReqVO pageVO) {
        SmsCodePageReqDTO codePageReqDTO=new SmsCodePageReqDTO();
        BeanUtils.copyProperties(pageVO,codePageReqDTO);
        PageResult<SmsCodeDO> page = smsCodeService.selectPage(codePageReqDTO);
        PageResult<SmsCodeRespVO> pageResult=new PageResult<>();
        if (null!= pageResult ) {
            List<SmsCodeDO> list = page.getList();
            if(CollectionUtils.isNotEmpty(list)){
                List<String> mList = list.stream().map(m -> {
                    return m.getMobile();
                }).collect(Collectors.toList());
                List<CompanyDO> cList = companyService.getCompanyByMobiles(mList);
                List<SmsCodeRespVO> list1 = new ArrayList<>( page.getList().size() );
                for ( SmsCodeDO studentDO : list ) {
                    SmsCodeRespVO respVO=new SmsCodeRespVO();
                    BeanUtils.copyProperties(studentDO,respVO);
                    CompanyDO companyDO = cList.stream().filter(m -> (studentDO.getMobile().equals(m.getPhoneNumber()))).findFirst().orElse(null);
                    if(null!=companyDO){
                        respVO.setApplyUserName(companyDO.getApplyUserName());
                        respVO.setCompanyName(companyDO.getCompanyName());
                    }
                    list1.add(respVO);
                }
                pageResult.setList(list1);
            }
            pageResult.setTotal( page.getTotal() );
        }
        return success(pageResult);
    }


}
